#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from persistance import *


class AdminCms:

  def __init__(self):
    self.mDB=ManageDB(Glob.dbName, Glob.user, Glob.passwd, Glob.host)
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)

  def getAdminMenu(self):
    html_menu = "<tr>\n<td colspan='3'>\n<ul id='nav_admin'>\n<li><a onclick='newPage()' href='#'>Nouvelle Page</a></li>\n<li><a onclick='configuration()' href='#'>Configuration</a></li>\n<li><a onclick='listImg()' href='#'>Liste des Images</a></li>\n<li><a onclick='listUser()' href='#'>Liste Utilisateurs</a></li>\n<li><a onclick='listDirectories()' href='#'>Liste des Dossiers</a></li>\n<li><a onclick='listProduct()' href='#'>Liste des Produits</a></li>\n<li><a onclick='helpList()' href='#'>Aide</a></li>\n<li><a onclick='sqlFiles()' href='#'>DB</a></li>\n</ul>\n</td>\n</tr>\n"
    return html_menu
  
  ## id=row[0] page_title=row[1] page_content=row[2] nav_id=row[3]
  def getPagesList(self):
    html_list = "<tr><td colspan='3'><br/><h3>Liste des pages&nbsp;&nbsp;<a href='#' onclick='newPage()' ><img src='img/add_icon.gif' title='Nouvelle page' width='16' height='16' ></a></h3><br/><table border='0' cellspacing='0' cellpadding='0' align='center' width='100%' class='pages_list'>\n<tr class='headerList'>\n<td><b>Titre de la Page</b></td>\n<td colspan='2'><b>Actions</b></td>\n</tr>\n"
    if (self.mDB.executSQL(Glob.select_all_from_page) == 0):
      logging.debug(u'AdminCms getPagesList SQL request : %s' % Glob.select_all_from_page)
    else:
      result=self.mDB.resultSQL()
      for row in result:
        html_list += "<tr>\n<td>%s</td>\n<td><a href'#' id='%s' onclick='modifyPage(this.id)'><img src='img/edit.gif' width='16' height='16' title='Modifier la Page'></a></td>\n<td><a href'#' id='%s' onclick='deletePage(this.id)'><img src='img/delete_icon.gif' width='17' height='16' title='Supprimer la Page'></a></td>\n</tr>" % (row[1].replace('"', '\\"').replace("'", "\\'"), row[0], row[0])
    html_list += "</table></td></tr>"
    return html_list

  def getPageHeader(self):
    html_header = "<table border='0' cellspacing='0' cellpadding='0' align='center' width='100%' class='pages_admin'>\n"
    return html_header

  def getPageFooter(self):
    html_footer = "</table>\n"
    return html_footer
